package com.dingqb.ktdemo.oop.companion_obj

class Myclass {
	companion object Factory {
		fun getName(): String = "伴生对象";
		var age: String = "1919"
	}

	// 私有函数，所以其返回类型是匿名对象类型
	private fun getObj() = object {
		val x: Int = 1212
	}

	fun getoobj() {
		var x = getObj().x;
		println(x);
	}
}

var myclass = Myclass.getName()
var myclass2 = Myclass.Factory.getName();

class MyClass1 {
	companion object {
		var t = "tt";
	}
}

val x = MyClass1.Companion


interface Factory<T> {
	fun create(): T
}


class MyClass3 {
	companion object : Factory<MyClass3> {
		//override fun create():MyClass3{ return MyClass3()}
		override fun create(): MyClass3 = MyClass3()//

		fun pri() {
			println("prin()")
		}
	}

	fun myM() {
		println("---my m -- ");

	}
}

class Student(name: String) : Factory<Student> {
	var n = name;
	override fun create(): Student {
		return Student(n);
	}
}


fun main(args: Array<String>) {
	println(myclass)
	println(Myclass.age)
	var mc = Myclass();
	mc.getoobj()
	println(x.t)

	println("\n\n")
	println(MyClass3.pri())
	var myclass3 = MyClass3.create();
	myclass3.myM();
	var v = MyClass3();
	println(v.myM())

	println(Student("this is a fanxing class").create().n)


}